package com.oneraku.service.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oneraku.service.domain.AddressInfo;
import com.oneraku.service.dao.AddressInfoMapper;
import com.oneraku.service.exceptions.InfoException;
import com.oneraku.service.service.IAddressInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneraku.service.utils.AuthBean;
import com.oneraku.service.utils.R;
import com.oneraku.service.utils.RequestUserUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 收货地址 服务实现类
 * </p>
 *
 * @author renhui <337375464@qq.com>
 * @since 2020-02-25
 */
@Service
public class AddressInfoServiceImpl extends ServiceImpl<AddressInfoMapper, AddressInfo> implements IAddressInfoService {
    private void checkAddress(AddressInfo addressInfo){
        if(StringUtils.isEmpty(addressInfo.getCountry())){
            throw new InfoException("收货地址国家不能为空");
        }
        if(addressInfo.getCountry().length() > 64){
            throw new InfoException("收货地址国家最多允许64个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getProvince())){
            throw new InfoException("收货地址省不能为空");
        }
        if(addressInfo.getProvince().length() > 64){
            throw new InfoException("收货地址省最多允许64个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getCity())){
            throw new InfoException("收货地址市不能为空");
        }
        if(addressInfo.getCity().length() > 64){
            throw new InfoException("收货地址市最多允许64个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getDistrict())){
            throw new InfoException("收货地址区不能为空");
        }
        if(addressInfo.getDistrict().length() > 64){
            throw new InfoException("收货地址区最多允许64个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getDetail())){
            throw new InfoException("收货地址详情不能为空");
        }
        if(addressInfo.getDetail().length() > 256){
            throw new InfoException("收货地址区最多允许256个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getContacts())){
            throw new InfoException("收货地址联系人不能为空");
        }
        if(addressInfo.getContacts().length() > 256){
            throw new InfoException("联系人最多允许256个字符");
        }
        if(StringUtils.isEmpty(addressInfo.getTelephone())){
            throw new InfoException("收货地址联系电话不能为空");
        }
        if(addressInfo.getTelephone().length() > 128){
            throw new InfoException("联系电话最多允许128个字符");
        }
    }
    @Override
    public R listAddress(AddressInfo addressInfo) {
        AuthBean authBean = RequestUserUtils.getUser();
        return R.ok(list(Wrappers.<AddressInfo>lambdaQuery().eq(AddressInfo::getUid,authBean.getId())));
    }
    @Override
    public R editAddress(AddressInfo addressInfo) {
        if(StringUtils.isEmpty(addressInfo.getId())){
            throw new InfoException("更新表示未知！");
        }
        checkAddress(addressInfo);
        AuthBean authBean = RequestUserUtils.getUser();
        if(addressInfo.getDefaults()){
            update(Wrappers.<AddressInfo>lambdaUpdate().set(AddressInfo::getDefaults,false).eq(AddressInfo::getUid,authBean.getId()));

        }
        boolean update = update(addressInfo, Wrappers.<AddressInfo>lambdaUpdate().eq(AddressInfo::getId,addressInfo.getId()).eq(AddressInfo::getUid,authBean.getId()));
        return R.ok(update);
    }
    @Override
    public R delAddress(AddressInfo addressInfo) {
        if(StringUtils.isEmpty(addressInfo.getId())){
            throw new InfoException("删除标识未知！");
        }
        AuthBean authBean = RequestUserUtils.getUser();
        AddressInfo localAddressInfo = getById(addressInfo.getId());
        if(null == localAddressInfo){
            throw new InfoException("未找到相关的对象");
        }
        if(!localAddressInfo.getUid().equals(authBean.getId())){
            throw new InfoException("无权限删除该收货地址");
        }
        return R.ok(removeById(localAddressInfo.getId()));
    }
    @Override
    public R addAddress(AddressInfo addressInfo) {
        checkAddress(addressInfo);
        AuthBean authBean = RequestUserUtils.getUser();
        if(addressInfo.getDefaults()){
            update(Wrappers.<AddressInfo>lambdaUpdate().set(AddressInfo::getDefaults,false).eq(AddressInfo::getUid,authBean.getId()));
        }
        addressInfo.setUid(authBean.getId());
        return R.ok(save(addressInfo));
    }
}
